<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="ISO-8859-1"%>
<%@ page import = "quiz.Quiz" %>
<%@ page import = "quiz.Account" %>
<%@ page import = "quiz.Result" %>
<%@ page import = "java.util.ArrayList" %>
<%
	Quiz q = (Quiz)request.getAttribute("selectedQuiz");
	String page_title = q.getName()+" | Quizzard";
	int quizID = q.getID();
	
	Result avg = (Result)request.getAttribute("summary");
	ArrayList<Result> topPerformers = (ArrayList<Result>)request.getAttribute("topPerformers");
	ArrayList<Result> topPerformersToday = (ArrayList<Result>)request.getAttribute("topPerformersInDay");
	ArrayList<Result> recentResults = (ArrayList<Result>)request.getAttribute("allRecentResults");
	ArrayList<Result> userResults = (ArrayList<Result>)request.getAttribute("userResultsOnQuiz");
	

	
	//TODO: add more options once servlet is created
%>
<jsp:include page="templates/header.jsp" flush="true">
	<jsp:param value = "<%= page_title %>" name = "title" />
</jsp:include>
<jsp:include page="templates/title_bar.jsp" flush = "true">
	<jsp:param value = "<%= q.getName() %>" name = "title" />
</jsp:include>
<div id = "content">
<div class = "center">
<div class = "bottom_border" style = "margin-bottom: 10px;"><div class = "description"><%= q.getDescription() %></div>
<div>Creator: 
<%
	if (q.getCreator().getID() < 0) {
		out.println(q.getCreator().getName());
	} else {
%>
<a href = "Profile?id=<%= q.getCreator().getID() %>"><%= q.getCreator().getName() %></a>
<% } %>
</div>
<div>Average Score: <%= avg.getScore() %></div>
</div>
<div class = "section_title">Recent Results</div>
<div class = "invisible_split_left"><div class = "section_title_small">Your recent results</div>
<%
	if (userResults.size() == 0) {
		if (session.getAttribute("account") == null) out.println("<a href = \"Login\">Log in</a> to track results");
		else out.println("You haven't take this quiz yet");
	}
	for(Result r: userResults) {
		out.println("<div><a href = \"Profile/?id="+r.getUser().getID()+"\">"+r.getUser().getName()+"</a>: "+r.getScore()+"</div>");
	}
%>
</div>
<div class = "invisible_split_right"><div class = "section_title_small">All recent results</div>
<%
	if (recentResults == null || recentResults.size() == 0) out.println("No recent results");
	for(Result r: recentResults) {
		out.print("<div>");
		if (r.getUser().getID() != -1) out.print("<a href = \"Profile?id="+r.getUser().getID()+"\">");
		out.print(r.getUser().getName());
		if (r.getUser().getID() != -1) out.print("</a>");
		out.println(": "+r.getScore()+"</div>");
	}
%>
</div>
<div class = "clear padding"></div>
<div class = "section_title">Top Scores</div>
<div class = "invisible_split_left"><div class = "section_title_small">Top scores today</div>
<%
	if (topPerformersToday == null || topPerformersToday.size() == 0) out.println("No top results");
	for(Result r: topPerformersToday) {
		out.print("<div>");
		if (r.getUser().getID() != -1) out.print("<a href = \"Profile?id="+r.getUser().getID()+"\">");
		out.print(r.getUser().getName());
		if (r.getUser().getID() != -1) out.print("</a>");
		out.println(": "+r.getScore()+"</div>");
	}
%>
</div>
<div class = "invisible_split_right"><div class = "section_title_small">Top scores of all time</div>
<%
	if (topPerformers == null || topPerformers.size() == 0) out.println("No top results");
	for(Result r: topPerformers) {
		out.print("<div>");
		if (r.getUser().getID() != -1) out.print("<a href = \"Profile?id="+r.getUser().getID()+"\">");
		out.print(r.getUser().getName());
		if (r.getUser().getID() != -1) out.print("</a>");
		out.println(": "+r.getScore()+"</div>");
	}
%>
</div>
<div class = "clear"></div>
<p><a href = "Take?quizID=<%=quizID%>"><span class = "btn">Take Quiz</span></a> 
<%
	if (q.has_practiceMode()){
%>
<a href = "Take?quizID=<%=quizID%>&practiceMode=true"><span class = "btn">Take Quiz in Practice Mode</span></a>
<%
	}

	if (session.getAttribute("account") != null) {
		Account acct = (Account)session.getAttribute("account");
		if (q.getCreator().getID() == acct.getID()) 
			out.println("<a href = \"AddQuestion?id="+q.getID()+"\"><span class = \"btn\">Edit Quiz</span></a> ");
		if (acct.isAdmin()) {
			out.println("<a href = \"ClearQuizHistory?id="+q.getID()+"\"><span class = \"btn\">Clear Quiz History</span></a> ");
			out.println("<a href = \"RemoveQuiz?id="+q.getID()+"\"><span class = \"btn\">Remove Quiz</span></a>");
		}
	}
%>
</div>
</div>
<jsp:include page="templates/footer.jsp" flush="true" />